package com.hellobike.haxing.modules.supplier.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.modules.product.entity.Category;
import com.hellobike.haxing.modules.product.mapper.CategoryMapper;
import com.hellobike.haxing.modules.supplier.entity.TSupplierClPro;
import com.hellobike.haxing.modules.supplier.mapper.TSupplierClProMapper;
import com.hellobike.haxing.modules.supplier.service.ITSupplierClProService;

import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 供应商商品表
 * @Author: haxing-boot
 * @Date: 2020-01-15
 * @Version: V1.0
 */
@Service
public class TSupplierClProServiceImpl extends ServiceImpl<TSupplierClProMapper, TSupplierClPro>
        implements ITSupplierClProService {

    @Autowired
    private TSupplierClProMapper tSupplierClProMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<TSupplierClPro> getBySupplierId(Long headerId) {
        QueryWrapper<TSupplierClPro> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supplier_id", headerId);
        List<TSupplierClPro> tSupplierClPros = tSupplierClProMapper.selectList(queryWrapper);
        fillCategoryNames(tSupplierClPros);
        return tSupplierClPros;
    }

    // 填充分类名称
    private void fillCategoryNames(List<TSupplierClPro> tSupplierClPros) {
        if (tSupplierClPros != null && tSupplierClPros.size() > 0) {
            Set<Long> categoryIds = new HashSet<>();
            tSupplierClPros.forEach(tSupplierClPro -> {
                if (tSupplierClPro.getFirstCategory() != null && tSupplierClPro.getSecondCategory() != null
                        && tSupplierClPro.getThirdCategory() != null) {
                    categoryIds.add(Long.valueOf(tSupplierClPro.getFirstCategory()));
                    categoryIds.add(Long.valueOf(tSupplierClPro.getSecondCategory()));
                    categoryIds.add(Long.valueOf(tSupplierClPro.getThirdCategory()));
                }
            });
            List<Category> categoryList = categoryMapper.selectBatchIds(categoryIds);
            Map<Long, String> categoryNameMap = categoryList.stream()
                    .collect(Collectors.toMap(Category::getId, Category::getName));
            tSupplierClPros.forEach(tSupplierClPro -> {
                if (tSupplierClPro.getFirstCategory() != null && tSupplierClPro.getSecondCategory() != null
                        && tSupplierClPro.getThirdCategory() != null) {
                    tSupplierClPro
                            .setFirstCategoryName(categoryNameMap.get(Long.valueOf(tSupplierClPro.getFirstCategory())));
                    tSupplierClPro.setSecondCategoryName(
                            categoryNameMap.get(Long.valueOf(tSupplierClPro.getSecondCategory())));
                    tSupplierClPro
                            .setThirdCategoryName(categoryNameMap.get(Long.valueOf(tSupplierClPro.getThirdCategory())));
                }
            });
        }
    }

    @Override
    public void updateClp(Long skuId, BigDecimal purchasePrice) {
        tSupplierClProMapper.updateClp(skuId, purchasePrice);
    }

    @Override
    public void updateClpDefault(Long skuId, Long supplierId, Integer isDefault) {
        if (skuId == null || supplierId == null) {
            return;
        }
        List<TSupplierClPro> supplierList = tSupplierClProMapper.selectList(new QueryWrapper<TSupplierClPro>().lambda()
                .eq(TSupplierClPro::getSkuId, skuId).eq(TSupplierClPro::getDeleted, 0));
        /**
         * 设置不为null
         */
        if (!CollectionUtils.isEmpty(supplierList)) {
            for (TSupplierClPro tSupplierClPro : supplierList) {
//                if (!tSupplierClPro.getSupplierId().equals(supplierId)) {
                TSupplierClPro param = new TSupplierClPro();
                param.setId(tSupplierClPro.getId());
                param.setIsDefault(0);
                tSupplierClProMapper.updateById(param);
//                }
            }
        }
        tSupplierClProMapper.updateClpDefault(skuId, supplierId, isDefault);
    }

}
